package org.darkforest.common.services.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.darkforest.common.services.BalancePayService;
import org.darkforest.common.services.MemberService;
import org.darkforest.common.services.PdLogService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.InvertCodeGenerator;
import org.darkforest.dao.mapper.OrderCommonMapper;
import org.darkforest.dao.mapper.OrderGiftCodeMapper;
import org.darkforest.dao.mapper.OrderMapper;
import org.darkforest.dao.mapper.OrderPayMapper;
import org.darkforest.model.*;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2017/4/25.
 */
@Service
public class BalancePayServiceImpl implements BalancePayService {

    @Autowired
    private OrderMapper orderMapper;


    @Autowired
    private MemberService memberService;

    @Autowired
    private OrderPayMapper orderPayMapper;

    @Autowired
    private PdLogService pdLogService;

    @Autowired
    private OrderCommonMapper orderCommonMapper;


    @Autowired
    private OrderGiftCodeMapper orderGiftCodeMapper;


    @Override
    @Transactional
    public AjaxReturnData doBalancePay(Integer orderId, String payPwd) {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        //检查订单状态是否符合
        //支付密码是否正确
        //检查余额是否足够
        Order order = orderMapper.selectByPrimaryKey(orderId);
        MemberWithBLOBs memberWithBLOBs = memberService.selectByPrimaryKey(order.getBuyerId());
        if(!"10".equals(order.getOrderState())){
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("订单状态异常");
        }else if(!DigestUtils.md5Hex(payPwd).equals(memberWithBLOBs.getMemberPaypwd())){
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("支付密码错误");
        }else if(memberWithBLOBs.getAvailablePredeposit().compareTo(order.getOrderAmount())<0){
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("账户余额不足");
        }else{

            //订单附属数据
            OrderCommon orderCommon =orderCommonMapper.selectByPrimaryKey(order.getOrderId());

            //支付扣款
            memberWithBLOBs.setAvailablePredeposit(memberWithBLOBs.getAvailablePredeposit().subtract(order.getOrderAmount()));
            memberService.updateByPrimaryKeyWithBLOBs(memberWithBLOBs);



            //支付表状态修改
            OrderPay orderPay = orderPayMapper.selectByOrderSn(order.getOrderSn());
            orderPay.setApiPayState("1");
            orderPay.setPayType(3);
            orderPay.setBankType("balance");
            orderPayMapper.updateByPrimaryKeySelective(orderPay);

            //用户余额变动记录
            PdLog pdLog = new PdLog();
            pdLog.setLgDesc("下单支付扣款,订单号"+order.getOrderSn());
            pdLog.setLgAddTime(DateUtil.unixTimeStampInt().intValue());
            pdLog.setLgType("order_pay");
            pdLog.setLgAvAmount(new BigDecimal(0.00).subtract(order.getOrderAmount()));
            pdLog.setLgFreezeAmount(new BigDecimal(0.00));
            pdLog.setLgOrderId(order.getOrderId());
            pdLog.setLgMemberId(memberWithBLOBs.getMemberId());
            pdLog.setLgMemberName(memberWithBLOBs.getMemberName());
            pdLog.setLgBalance(memberWithBLOBs.getAvailablePredeposit());
            pdLogService.insertSelective(pdLog);


            //如果是礼品码送则生成礼品码
            if(orderCommon.getPostType()==2){
                for(int i=orderCommon.getCodeNum();i>=1;i--){
                    OrderGiftCode orderGiftCode = new OrderGiftCode();
                    orderGiftCode.setGenerateOrderId(order.getOrderId().toString());
                    orderGiftCode.setCreateName(memberWithBLOBs.getMemberName());
                    orderGiftCode.setCreateTime(DateUtil.getCurrentDateTime());
                    orderGiftCode.setCreaterId(memberWithBLOBs.getMemberId());
                    orderGiftCode.setCodeState(1);

                    List<String> gc = InvertCodeGenerator.genCodes(6, 1);
                    int count = orderGiftCodeMapper.selectCountByCode(gc.get(0));
                    while(count>0){
                        gc = InvertCodeGenerator.genCodes(6,1);
                    }
                    orderGiftCode.setGiftCode(gc.get(0));
                    orderGiftCodeMapper.insertSelective(orderGiftCode);
                }

                //修改订单状态 //礼品码方式30
                order.setOrderState("30");
            }else{
                //邮寄方式20
                order.setOrderState("20");
            }
            order.setPaymentTime(DateUtil.unixTimeStampInt().intValue());
            order.setPaymentCode("2");

            orderMapper.updateByPrimaryKeySelective(order);
            //
            ajaxReturnData.setResult(true);
            ajaxReturnData.setMessage("支付成功");
        }












        return ajaxReturnData;
    }
}
